package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

public class Main extends Application {

    @Override
    public void start(Stage stage) {

        stage.setTitle("sql转换工具");

        Scene scene = new Scene(new Group(), 650, 420);
        Button button = new Button();
        button.setText("转换");
        button.setPrefWidth(150);
        button.setPrefHeight(50);

        Button clear = new Button();
        clear.setText("清空上面");
        clear.setPrefWidth(150);
        clear.setPrefHeight(50);

        Button copy = new Button();
        copy.setText("复制sql");
        copy.setPrefWidth(150);
        copy.setPrefHeight(50);

        TextArea textArea = new TextArea();
        textArea.setPrefHeight(150);
        textArea.setPrefWidth(500);
        textArea.setWrapText(true);

        TextArea area = new TextArea();
        area.setPrefHeight(150);
        area.setPrefWidth(500);
        area.setWrapText(true);

        Label label = new Label();

        getText(button, textArea, area, label);
        clear.setOnAction(event -> {
            textArea.clear();
            label.setText("");
        });
        copy.setOnAction(event -> {
            if (!area.getText().equals("")) {
                Clipboard clipboard = Clipboard.getSystemClipboard();
                ClipboardContent clipboardContent = new ClipboardContent();
                clipboardContent.putString(area.getText());
                clipboard.setContent(clipboardContent);
                label.setText("复制成功");
            }
        });

        GridPane grid2 = new GridPane();
        grid2.setVgap(4);
        grid2.setHgap(10);
        grid2.setPadding(new Insets(5, 0, 5, 0));
        grid2.add(button, 1, 1);
        grid2.add(clear, 2, 1);
        grid2.add(copy, 3, 1);

        GridPane grid = new GridPane();
        grid.setVgap(4);
        grid.setHgap(10);
        grid.setPadding(new Insets(5, 5, 5, 5));
        grid.add(new Label("请输入文本: "), 0, 0);
        grid.add(new Label("结果: "), 0, 2);
        grid.add(textArea, 1, 0);
        grid.add(grid2, 1, 1);
        grid.add(area, 1, 2);
        grid.add(label, 1, 3);
        Group root = (Group) scene.getRoot();
        root.getChildren().add(grid);

        //设置窗口的图标.
        stage.getIcons().add(new Image("/sample/icon.jpg"));
        stage.setScene(scene);
        stage.show();
    }

    private void getText(Button button, TextArea source, TextArea target, Label label) {
        button.setOnAction(e -> {
            String sql = getSql(source.getText());
            System.out.println(source.getText());
            target.setText(sql);
            label.setText("");
        });
    }

    public static void main(String[] args) {
        launch(args);
    }

    public static String getSql(String str) {
        if (str.trim().equals("")) {
            return "请输入内容";
        }
        String sql = "";
        try {
            if (!str.contains("Preparing")) {
                return "Preparing 参数不存在";
            }
            if (!str.contains("Parameters")) {
                return "Parameters 参数不存在";
            }
            int preparing = str.indexOf("Preparing: ");
            int start = str.indexOf("\n");
            sql = str.substring(preparing, start).replace("Preparing: ", "");
            String aram = str.substring(str.indexOf("Parameters: "));
            String[] split = aram.substring(0, !aram.contains("\n") ? aram.length() : aram.indexOf("\n"))
                    .replace("Parameters: ", "").split(",");
            for (String s : split) {
                if (s.contains("String")) {
                    s = s.trim().replace(s.substring(s.indexOf("("), s.indexOf(")") + 1), "");
                    s = s.contains("\n") ? s.substring(0, s.indexOf("\n")) : s;
                    s = "'" + s + "'";
                } else {
                    s = s.trim().replace(s.substring(s.indexOf("("), s.indexOf(")") + 1), "");
                    s = s.contains("\n") ? s.substring(0, s.indexOf("\n")) : s;
                }
                if (s.contains("Timestamp")) {

                }
                sql = sql.replaceFirst("\\?", s);

            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return " 格式化失败" + e.getMessage();
        }

        return sql.replace("\\n", "");
    }

}
